#include "Protocol.hpp"
#include "Sock.hpp"

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cout << "Usage:\n\t" << "server_ip server _port" << endl;
        return 1;
    }

    int sock = sock::Socket();
    sock::Connect(sock, argv[1], atoi(argv[2]));

    // 业务逻辑
    request_t req;
    cout << "请根据形如这样的示例(1+1)输入数据方法# ";
    cin >> req.x >> req.op >> req.y;
    string json_string = SerializeRequst(req);//序列化
    ssize_t s = write(sock, json_string.c_str(), json_string.size());
    
    char buffer[1024];
    s = read(sock, &buffer, sizeof(buffer) - 1);
    if (s > 0)
    {
        string str = buffer;
        response_t res;
        DeserializeResponse(str, res);

        if (res.code != 0)
        {
            cout << "请求错误！" << res.code << endl;
        }
        else
        {
            cout << "result: " << res.result << endl;
        }
    }
    
    // response_t res;
    // s = read(sock, &res, sizeof(res));
    // if (s == sizeof(res))
    // {
    //     if (res.code != 0)
    //     {
    //         cout << "请求错误！" << res.code << endl;
    //     }
    //     else
    //     {
    //         cout << "result: " << res.result << endl;
    //     }
    // }

    return 0;
}